package cn.edu.bjut.hrglxt;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class LoginAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		LoginForm loginForm=(LoginForm)form;
//		if(loginForm.getUsername().equals("cxp")){
//			return mapping.findForward("loginSuccess");
//		}else{
//			return mapping.findForward("loginFailure");
//		}
		PrintWriter out=response.getWriter();
						String username=loginForm.getUsername();
						if(username.equals(null)) username="null";
						String password=loginForm.getPassword();
						 DataSource ds = getDataSource(request,"datasource");
						 Connection conn=ds.getConnection();
						 Statement stmt=conn.createStatement();
						 String sqlstr="select * from loginuser where " +
						 		"username='"+username+"' and password='"+password+"'";
						 ResultSet rs=stmt.executeQuery(sqlstr);
						 boolean result=false;
						 if(rs.next())
						 		{
							 		result=true;
							 		
						 		}
						 else result= false;
						 conn.close();
						 request.setAttribute("username", username);
						 if(result==true)
						 		{
							 out.write(request.getAttribute("username").toString());
							 return mapping.findForward("welcome");
							 
						 		}
						 
 				         
						 else { 
							 
 
							 return mapping.findForward("input");}
						 }
//						 
//		 
//		PrintWriter out=response.getWriter();
//		out.write("<html>\r\n");
//		out.write("\r\n");
//		out.write("<body>\r\n");
//		out.write("Hello Wold!");
//		out.write("</body>\r\n");
//		out.write("</html>");
		
// 	return null;
		//return super.execute(mapping, form, request, response);
	}

 














